// Copyright (c) 2010, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// ---
// Author: Jeffrey Junfeng Pan (panjunfeng@gmail.com)

#include "header.h"

#include <algorithm>

#include "common/logging.h"

namespace frequent_pattern_mining {

// Constructor.
Header::Header()
  : header_table_(new HeaderTable),
    frequency_table_(new FrequencyTable) {
}

// Destructor.
Header::~Header() {
}

// Get the frequency of a given item index.
int Header::frequency(int index) const {
  unordered_map<int, int>::const_iterator iter = frequency_table_->find(index);
  if (iter == frequency_table_->end()) {
    return 0;
  } else {
    return iter->second;
  }
}

// Set the frequency of an item index
void Header::set_frequency(int index, int frequency) {
  (*frequency_table_)[index] = frequency;
}

// Increase the total frequency of an item with the given amount.
void Header::add_frequency(int index, int frequency) {
  FrequencyTable::const_iterator iter = frequency_table_->find(index);
  if (iter == frequency_table_->end()) {
    // new index
    (*frequency_table_)[index] = frequency;
  } else {
    // existing index
    (*frequency_table_)[index] += frequency;
  }
}

// Get the header node given an item index
Node* Header::header_node(int index) const {
  HeaderTable::const_iterator iter = header_table_->find(index);
  if (iter == header_table_->end()) {
    return NULL;
  } else {
    return iter->second;
  }
}

// Set the header node in the header table to the given value.
// Note: this function will not link the nodes with the same
// index together. Instead, it will replace the node in the header table.
// If you want to add a node to an existing link list in the header table,
// use add_node() function.
void Header::set_header_node(Node* node) {
  CHECK(node) << ": node can not be NULL";
  (*header_table_)[node->index()] = node;
}

// Add a Node to the header table.
void Header::add_node(Node* node) {
  CHECK(node) << ": node can not be NULL";
  int index = node->index();
  HeaderTable::const_iterator iter = header_table_->find(index);
  if (iter == header_table_->end()) {
    // a new node
    (*header_table_)[index] = node;
  } else {
    // existing node
    node->set_header_next(iter->second);
    (*header_table_)[index] = node;
  }
}

// Get the actual header table
const HeaderTable& Header::header_table() const {
  return *header_table_;
}

// Get the actual frequency table
const FrequencyTable& Header::frequency_table() const {
  return *frequency_table_;
}
}  // namepsace frequent_pattern_mining
